package com.xxxx.generator;

import com.xxxx.generator.mygenerator.Module;

import java.io.File;

/**
 * clean 清空generator层的生成数据
 *          禁止修改
 * @author ppliang
 * @date 2020/12/31 15:51
 */
public class Clean {

    //删除的模块
    private final static String module= Module.generator.toString();
    //项目中间路径
    private final static String midPath="\\shop-"+module+"\\src\\main\\java\\com\\xxxx\\"+module+"\\";
    //清除模块下文件夹
    private final static String[] paths={"controller","mapper","pojo","service"};

    public static void main(String[] args) {
        for (String path:paths){
            File file = getFile(path);
            deleteFiles(file);
        }
        //删除mapper下的文件
        clearResources();
        System.out.println(module+"数据已清空");
    }

    //删除mapper下的文件
    private static boolean deleteFiles(File directory){
        File [] files = directory.listFiles();
        if(files==null||files.length<1){
            return true;
        }
        for (File file:files){
            if(file.isDirectory()){
                deleteFiles(file);
            }else {
                file.delete();
            }
        }
        return true;
    }

    private static void clearResources(){
        String path=System.getProperty("user.dir")+"\\shop-"+module+"\\src\\main\\resources\\mapper";
        File file = new File(path);
        deleteFiles(file);
    }
    //获取文件
    private static File getFile(String name){
        String path=System.getProperty("user.dir")+midPath+name;
        File file = new File(path);
        return file;
    }
}
